package com.takeaway.ui.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.daimajia.slider.library.SliderLayout
import com.daimajia.slider.library.SliderTypes.BaseSliderView
import com.daimajia.slider.library.SliderTypes.TextSliderView
import com.takeaway.R


class HomeRcAdapter(var context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        // 定义常量
        val TYPE_TITLE = 0
        val TYPE_SELLR = 1
    }
    private var datas: ArrayList<String> = ArrayList()

    fun setData(datas: ArrayList<String>) {
        this.datas = datas
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(p0: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when(viewType) {
            TYPE_TITLE -> return TitleViewHolder(View.inflate(context, R.layout.item_title, null))
            TYPE_SELLR -> return HomeItemViewHolder(View.inflate(context, R.layout.item_seller, null))
            else -> return HomeItemViewHolder(View.inflate(context, R.layout.item_home_common, null))
        }
    }

    override fun getItemCount(): Int {
        if (datas.size > 0) {
            return datas.size + 1
        } else {
            return 0
        }
    }

    override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
        val viewType = getItemViewType(p1)
        when(viewType) {
            TYPE_TITLE -> (p0 as TitleViewHolder).bindData("我是headder")
            TYPE_SELLR -> (p0 as HomeItemViewHolder).bindData(datas[p1 - 1])
        }

    }

    /// 实现不同类型
    override fun getItemViewType(position: Int): Int {
        if (position == 0) {
            return TYPE_TITLE
        } else {
            return TYPE_SELLR
        }
    }



    inner class HomeItemViewHolder(item: View) : RecyclerView.ViewHolder(item) {


        init {
        }

        fun bindData(data: String) {
        }
    }

    private var sliderData: HashMap<String, String> = HashMap()

    inner class TitleViewHolder(item: View) : RecyclerView.ViewHolder(item) {

        var slider: SliderLayout
        init {
            slider = item.findViewById(R.id.slider)
        }

        fun bindData(data: String) {
            sliderData.put("Hannibal", "https://imgsa.baidu.com/news/q%3D100/sign=51643253012442a7a80ef9a5e142ad95/9f2f070828381f3026118579a7014c086f06f0d2.jpg")
            sliderData.put("Big Bang Theory", "https://imgsa.baidu.com/news/q%3D100/sign=8d1af2c307f79052e91f433e3cf1d738/a044ad345982b2b77d1cb8143fadcbef77099b5b.jpg")
            sliderData.put("House of Cards", "https://imgsa.baidu.com/news/q%3D100/sign=dc16dbe613950a7b73354ac43ad0625c/6159252dd42a2834a485b6db55b5c9ea15cebf3b.jpg")
            sliderData.put("Game of Thrones", "https://imgsa.baidu.com/news/q%3D100/sign=ead78c7b0d3b5bb5b8d724fe06d1d523/4ec2d5628535e5dd25f2280f78c6a7efcf1b6200.jpg")

            for ((key, value) in sliderData) {
                val textSliderView = TextSliderView(context)
                textSliderView.description(key).image(value)
                slider.addSlider(textSliderView)
            }
        }
    }
}